﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using XsClient.Common;

namespace XsClient.GUI
{
    /// <summary>
    /// WinSetRegex.xaml 的交互逻辑
    /// </summary>
    public partial class WinSetRegex : Window
    {
        public WinSetRegex()
        {
            InitializeComponent();
            Init();
        }
        ContextFilter config = null;
        private void Init()
        {
            try
            {
                lbRegexs.ItemsSource = null;
                config = ContextFilter.Current;
                if (config == null)
                    config = new ContextFilter() { Filters = new List<FilterRegex>() };
                if (config.Filters == null)
                    config.Filters = new List<FilterRegex>();
                lbRegexs.ItemsSource = config.Filters;
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }

        }
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FilterRegex filter = new FilterRegex();
                filter.Name = txtName.Text.Trim();
                filter.RegexStr = txtRegex.Text.Trim();
                if (string.IsNullOrEmpty(filter.Name) || string.IsNullOrEmpty(filter.RegexStr))
                {
                    return;
                }
                var item = config.Filters.SingleOrDefault(p => p.Name == filter.Name);
                if (item != null)
                {
                    int id = config.Filters.IndexOf(item);
                    config.Filters[id] = item;

                }
                else
                {
                    config.Filters.Add(filter);
                }
                config.Save();
                lbRegexs.ItemsSource = null;
                lbRegexs.ItemsSource = config.Filters;
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }

        private void LbRegexs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbRegexs.SelectedIndex >= 0)
                {
                    int id = lbRegexs.SelectedIndex;
                    if (config.Filters.Count <= id)
                        return;
                    var item = config.Filters[id];
                    txtName.Text = item.Name;
                    txtRegex.Text = item.RegexStr;
                }

            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }

        private void LbMiDel_Click(object sender, RoutedEventArgs e)
        {
            if (lbRegexs.SelectedIndex >= 0)
            {
                int id = lbRegexs.SelectedIndex;
                if (config.Filters.Count <= id)
                    return;
                var item = config.Filters[id];
                if (MessageBox.Show("删除" + item.Name + "?", "删除确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    try
                    {
                        config.Filters.Remove(item);
                        config.Save();
                    }
                    catch (Exception ex)
                    {
                        NewLife.Log.XTrace.WriteException(ex);
                    }
            }
        }
    }
}
